package top.tenebrous.termbor.rpc.proxy.jdk;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.tenebrous.termbor.rpc.proxy.consumer.Consumer;
import top.tenebrous.termbor.rpc.proxy.factory.BaseProxyFactory;
import top.tenebrous.termbor.rpc.proxy.factory.ProxyFactory;
import top.tenebrous.termbor.rpc.proxy.object.ObjectProxy;
import top.tenebrous.termbor.rpc.spi.SPIClass;

import java.lang.reflect.Proxy;

/**
 * @program: termbor-rpc
 * @description:
 * @author: yazhang6
 * @create: 2024-05-22 17:41
 **/
@SPIClass
public class JdkProxyFactory<T> extends BaseProxyFactory<T> implements ProxyFactory<T> {

    private static final Logger logger = LoggerFactory.getLogger(JdkProxyFactory.class);

    /**
     * 服务版本号
     */
    private String serviceVersion;

    /**
     * 服务分组
     */
    private String serviceGroup;

    /**
     * 超时时间，默认15s
     */
    private long timeout = 15000;

    /**
     * 服务消费者
     */
    private Consumer consumer;

    /**
     * 序列化类型
     */
    private String serializationType;

    /**
     * 是否异步调用
     */
    private boolean async;

    /**
     * 是否单向调用
     */
    private boolean oneway;

    public JdkProxyFactory(String serviceVersion, String serviceGroup, long timeout, Consumer consumer,
        String serializationType, boolean async, boolean oneway) {
        this.serviceVersion = serviceVersion;
        this.serviceGroup = serviceGroup;
        this.timeout = timeout;
        this.consumer = consumer;
        this.serializationType = serializationType;
        this.async = async;
        this.oneway = oneway;
    }

    public JdkProxyFactory() {
    }

    @Override
    public T getProxy(Class<T> clazz) {
        logger.info("基于jdk动态代理...");
        return (T) Proxy.newProxyInstance(
            clazz.getClassLoader(),
            new Class<?>[]{clazz},
            objectProxy
        );
    }
}
